package org.apache.ibatis.zhshuo;


import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.zhshuo.domain.User;
import org.apache.ibatis.zhshuo.mapper.MySqlMapper;

import javax.sql.DataSource;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.util.List;

/**
 * @author zhshuo
 * @date 2021/07/26 18:00
 **/
public class MainTest<T> {

 public static DataSource getMySqlDataSource(){
    UnpooledDataSource dataSource = new UnpooledDataSource();
    dataSource.setDriver("com.mysql.cj.jdbc.Driver");
    dataSource.setPassword("abc12345");
    dataSource.setUsername("root");
    dataSource.setUrl("jdbc:mysql://localhost:3306/test");
    return dataSource;
  }

  public static SqlSessionFactory createSqlSessionFactory(){
    Configuration configuration = new Configuration();
    DataSource mySqlDataSource = getMySqlDataSource();
    TransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
    configuration.setEnvironment(new Environment("dev",jdbcTransactionFactory,mySqlDataSource));
    configuration.addMappers("org.apache.ibatis.zhshuo.mapper");
    SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
    return sqlSessionFactory;
  }

  public static void main(String[] args) {
   SqlSessionFactory sqlSessionFactory = createSqlSessionFactory();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    MySqlMapper mapper = sqlSession.getMapper(MySqlMapper.class);
    List<User> users = mapper.selectOne(10,"zps");
    System.out.println(users);
  }

}
